﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dln.Common.Helper;
using Dln.Model;
using ThoughtWorks.QRCode.Codec;
using System.Drawing;
using ThoughtWorks.QRCode.Codec.Data;

public partial class Page_Customer_PatientEdit : BasePage
{
    public string openid = string.Empty;
    public PatientInfo Model = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        openid = HttpContext.Current.Request.QueryString["openid"];
        //openid = "oF6dUwxAa6BtWPqaRnE_lz7lDBgQ";
        Model = SqlHelper.Context.From<PatientInfo>().Where(d => d.Openid == openid && d.Delstatus == 0).ToFirstDefault();
        if (!string.IsNullOrEmpty(Model.QrCodeImg))
        {
        }
        else
        {
            var returnImg = CreateCode_Simple(Model.PatientPatiID);
            Model.QrCodeImg = returnImg;
            var updateResult = SqlHelper.Context.Update<PatientInfo>(Model);
        }

        if (!string.IsNullOrEmpty(Model.BarCodeImg))
        {
        }
        else
        {
            var returnImg = BuildBarcode(Model.PatientPatiID);
            Model.BarCodeImg = returnImg;
            var updateResult = SqlHelper.Context.Update<PatientInfo>(Model);
        }

    }

    private string CreateCode_Simple(string nr)
    {
        QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
        qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
        qrCodeEncoder.QRCodeScale = 4;
        qrCodeEncoder.QRCodeVersion = 8;
        qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
        //System.Drawing.Image image = qrCodeEncoder.Encode("4408810820 深圳－广州 小江");
        System.Drawing.Image image = qrCodeEncoder.Encode(nr);
        var filename = Guid.NewGuid().ToString() + ".jpg";
        string filepath = Server.MapPath(@"~\Images\CardIdImg") + "\\" + filename;
        System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
        image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);

        fs.Close();
        image.Dispose();
        return "/Images/CardIdImg/" + filename;
        //二维码解码
        //var codeDecoder = CodeDecoder(filepath);
    }
    /// <summary>
    /// 二维码解码
    /// </summary>
    /// <param name="filePath">图片路径</param>
    /// <returns></returns>
    public string CodeDecoder(string filePath)
    {
        if (!System.IO.File.Exists(filePath))
            return null;
        Bitmap myBitmap = new Bitmap(System.Drawing.Image.FromFile(filePath));
        QRCodeDecoder decoder = new QRCodeDecoder();
        string decodedString = decoder.decode(new QRCodeBitmapImage(myBitmap));
        return decodedString;
    }

    string BuildBarcode(string code)
    {
        System.Drawing.Image image;
        int width = 250, height = 70;
        //byte[] buffer = GetBarcode(height, width,
        //         BarcodeLib.TYPE.CODE128, "Avx-(13614)-vR", out image);
        return GetBarcode(height, width, BarcodeLib.TYPE.CODE128, code, out image);
    }

    string GetBarcode(int height, int width, BarcodeLib.TYPE type,
                                         string code, out System.Drawing.Image image)
    {
        image = null;
        BarcodeLib.Barcode b = new BarcodeLib.Barcode();
        b.BackColor = System.Drawing.Color.White;
        b.ForeColor = System.Drawing.Color.Black;
        b.IncludeLabel = true;
        b.Alignment = BarcodeLib.AlignmentPositions.CENTER;
        b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER;
        b.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
        System.Drawing.Font font = new System.Drawing.Font("verdana", 10f);
        b.LabelFont = font;

        b.Height = height;
        b.Width = width;

        image = b.Encode(type, code);
        return SaveImage(image, Guid.NewGuid().ToString("N") + ".png");
        //byte[] buffer = b.GetImageData(SaveTypes.GIF);
        //return buffer;
    }

    private string SaveImage(System.Drawing.Image image, string p)
    {
        //自动保存图片到指定目录中
        //string stringfilename = "G:\\" + p;
        var filename = Guid.NewGuid().ToString() + ".jpg";
        string filepath = Server.MapPath(@"~\Images\CardIdImg") + "\\" + filename;
        System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
        image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
        fs.Close();
        image.Dispose();
        return "/Images/CardIdImg/" + filename;
    }
}